package com.ionehe.group.common.uaa.ex.application.rest;

import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.uaa.ex.application.converter.RoleInfoConverter;
import com.ionehe.group.common.uaa.ex.application.model.RoleInfoAddVO;
import com.ionehe.group.common.uaa.ex.application.model.RoleInfoUpdateVO;
import com.ionehe.group.common.uaa.ex.application.model.RoleInfoVO;
import com.ionehe.group.common.uaa.ex.application.model.RoleQuery;
import com.ionehe.group.common.uaa.ex.domain.service.RoleInfoReadService;
import com.ionehe.group.common.uaa.ex.domain.service.RoleInfoWriteService;
import com.ionehe.group.common.uaa.ex.dto.Paging;
import com.ionehe.group.common.uaa.ex.dto.RoleInfoAddDTO;
import com.ionehe.group.common.uaa.ex.dto.RoleInfoDTO;
import com.ionehe.group.common.uaa.ex.dto.RoleInfoUpdateDTO;
import com.ionehe.group.common.uaa.ex.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * @USER:秀
 * @DATE: 2020/11/30
 * @PROJECT_NAME: general-permissions
 * @Description 角色信息服务
 **/
@RestController
@RequestMapping("/api/uaa/roleInfo")
@Api(hidden = true, value = "uaa-角色管理相关服务", tags = "uaa-角色管理相关服务")
@ApiIgnore
public class UaaRoleInfoController {
    @Autowired
    private RoleInfoReadService roleInfoReadService;
    @Autowired
    private RoleInfoWriteService roleInfoWriteService;
    @Resource
    private RoleInfoConverter roleInfoConverter;
    @Autowired
    private LoginUtils userUtil;


    @GetMapping("/list")
    @ApiOperation("角色列表")
    @ApiImplicitParam(name = "customerId", value = "客户id", example = "1", dataType = "Long", required = true, type = "query")
    public Response<List<RoleInfoVO>> list(){
        Long customerId = userUtil.getCurrentUser().getData().getUserInfoDTO().getCustomerId();
        if(customerId == null){
            return Response.no("customerId不能为空！");
        }
        Response<List<RoleInfoDTO>> response = roleInfoReadService.list(customerId);
        if(!response.success()){
            return Response.no(response.getMessage());
        }
        return Response.yes(roleInfoConverter.dtoList2vo(response.getData()));
    }


    @PostMapping("/page")
    @ApiOperation("角色列表")
    @ApiImplicitParam(name = "customerId", value = "客户id", example = "1", dataType = "Long", required = true, type = "query")
    public Response<Paging<RoleInfoDTO>> page(@RequestBody RoleQuery roleQuery){
        Long customerId = userUtil.getCurrentUser().getData().getUserInfoDTO().getCustomerId();
        if(customerId == null){
            return Response.no("customerId不能为空！");
        }
        roleQuery.setCustomerId(customerId);
        return roleInfoReadService.page(roleQuery);
    }


    /**
     * 新增角色
     * @param roleInfoAddVO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增角色")
    public Response<Boolean> add(@RequestBody RoleInfoAddVO roleInfoAddVO){
        if(StringUtils.isEmpty(roleInfoAddVO.getRemark()) || roleInfoAddVO.getRemark().length() > 30){
            return Response.no("备注不能为空并且不能超过30字！");
        }
        Long customerId = userUtil.getCurrentUser().getData().getUserInfoDTO().getCustomerId();

        roleInfoAddVO.setCustomerId(customerId);
        RoleInfoAddDTO roleInfoAddDTO = roleInfoConverter.vo2dto(roleInfoAddVO);
        return roleInfoWriteService.add(roleInfoAddDTO);
    }

    /**
     * 编辑角色
     * @param roleInfoUpdateVO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("编辑角色")
    public Response<Boolean> update(@RequestBody RoleInfoUpdateVO roleInfoUpdateVO){
        Long customerId = userUtil.getCurrentUser().getData().getUserInfoDTO().getCustomerId();
        roleInfoUpdateVO.setCustomerId(customerId);
        RoleInfoUpdateDTO roleInfoUpdateDTO = roleInfoConverter.vto2do(roleInfoUpdateVO);
        return roleInfoWriteService.update(roleInfoUpdateDTO);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("删除角色")
    public Response<Boolean> delete(@RequestParam("id") Long id){
        return roleInfoWriteService.delete(id);
    }

}
